<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 会员逻辑
 */
class Student extends AdminBase
{
    
    /**
     * 获取会员信息
     */
    public function getStudentInfo($where = [], $field = true)
    {
        
        $info = $this->modelStudent->getInfo($where, $field);
        
        $info['leader_nickname'] = $this->modelStudent->getValue(['id' => $info['leader_id']], 'nickname');
        
        return $info;
    }
    
    /**
     * 获取会员列表
     */
    public function getStudentList($where = [], $field = 'm.*,b.nickname as leader_nickname', $order = '', $paginate = DB_LIST_ROWS)
    {
        
        $this->modelStudent->alias('m');
        
        $join = [
                    [SYS_DB_PREFIX . 'student b', 'm.leader_id = b.id', 'LEFT'],
                ];
        
        $where['m.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelStudent->getList($where, $field, $order, $paginate, $join);
    }
    
    /**
     * 导出会员列表
     */
    public function exportStudentList($where = [], $field = 'm.*,b.nickname as leader_nickname', $order = '')
    {
        
        $list = $this->getStudentList($where, $field, $order, false);
        
        $titles = "昵称,用户名,邮箱,手机,注册时间,上级";
        $keys   = "nickname,username,email,mobile,create_time,leader_nickname";
        
        action_log('导出', '导出会员列表');
        
        export_excel($titles, $keys, $list, '会员列表');
    }
    
    /**
     * 获取会员列表搜索条件
     */
    /*public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['m.nickname|m.username|m.email|m.mobile'] = ['like', '%'.$data['search_data'].'%'];
        
        if (!is_administrator()) {
            
            $student = session('student_info');
            
            if ($student['is_share_student']) {
                
                $ids = $this->getInheritStudentIds(STUDENT_ID);
                
                $ids[] = STUDENT_ID;
                
                $where['m.leader_id'] = ['in', $ids];
                
            } else {
                
                $where['m.leader_id'] = STUDENT_ID;
            }
        }
        
        return $where;
    }*/
    
   
    /**
     * 会员添加
     */
    public function studentAdd($data = [])
    {
        
        $validate_result = $this->validateStudent->scene('add')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateStudent->getError()];
        }
        
        $url = url('studentList');
        
        $data['nickname']  = $data['username'];
       // $data['leader_id'] = STUDENT_ID;
        $data['is_inside'] = DATA_NORMAL;
        
        $result = $this->modelStudent->setInfo($data);
        
        $result && action_log('新增', '新增会员，username：' . $data['username']);
        
        return $result ? [RESULT_SUCCESS, '会员添加成功', $url] : [RESULT_ERROR, $this->modelStudent->getError()];
    }
    
    /**
     * 会员编辑
     */
    public function studentEdit($data = [])
    {
        
        $validate_result = $this->validateStudent->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateStudent->getError()];
        }
        
        $url = url('studentList');
        
        $result = $this->modelStudent->setInfo($data);
        
        $result && action_log('编辑', '编辑会员，id：' . $data['id']);
        
        return $result ? [RESULT_SUCCESS, '会员编辑成功', $url] : [RESULT_ERROR, $this->modelStudent->getError()];
    }
    
    /**
     * 设置会员信息
     */
    public function setStudentValue($where = [], $field = '', $value = '')
    {
        
        return $this->modelStudent->setFieldValue($where, $field, $value);
    }
    
    /**
     * 会员删除
     */
    public function studentDel($where = [])
    {
        
        $url = url('studentList');
        
        $result = $this->modelStudent->deleteInfo($where);
                
        $result && action_log('删除', '删除会员，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '会员删除成功', $url] : [RESULT_ERROR, $this->modelStudent->getError(), $url];
    }
}
